package com.open4cn.simulator.config.server.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.open4cn.simulator.config.entity.DeviceEntity;
import com.open4cn.simulator.config.mapper.DeviceMapper;
import com.open4cn.simulator.config.server.IDeviceServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author yinxiaopeng
 * @version 1.0.0
 * @company 致景科技
 * @department 飞梭BU/研发
 * @description
 * @Email yinxiaopeng@zj.tech
 * @date 2022-05-04 13:44
 */
@Service
public class DeviceServerImpl implements IDeviceServer {

    @Autowired
    private DeviceMapper deviceMapper;

    @Override
    public int add(DeviceEntity device) {
        device.setId(null);
        device.setCreateTime(LocalDateTime.now());
        device.setUpdateTime(LocalDateTime.now());
        device.setEnable(1);
        return deviceMapper.insert(device);
    }

    @Override
    public int update(DeviceEntity device) {
        DeviceEntity entity = deviceMapper.selectById(device.getId());
        if (null == entity) {
            return 0;
        }
        return deviceMapper.updateById(device);
    }

    @Override
    public DeviceEntity get(String id) {
        return deviceMapper.selectById(id);
    }

    @Override
    public List<DeviceEntity> list() {
        LambdaQueryWrapper<DeviceEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DeviceEntity::getEnable, 1);
        return deviceMapper.selectList(queryWrapper);
    }
}
